package m

import org.springframework.dao.DataIntegrityViolationException

class WindowsTaskController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def windowsTaskManager

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        def taskMap = windowsTaskManager.getTaskList()
        println "size:${taskMap.size()}"
        [windowsTaskInstanceList: taskMap]
    }

    def create() {
        [windowsTaskInstance: new WindowsTask(params)]
    }

    def save() {
        def windowsTaskInstance = new WindowsTask(params)
        if (!windowsTaskInstance.save(flush: true)) {
            render(view: "create", model: [windowsTaskInstance: windowsTaskInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'windowsTask.label', default: 'WindowsTask'), windowsTaskInstance.id])
        redirect(action: "show", id: windowsTaskInstance.id)
    }

    def show() {
        def windowsTaskInstance = WindowsTask.get(params.id)
        if (!windowsTaskInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'windowsTask.label', default: 'WindowsTask'), params.id])
            redirect(action: "list")
            return
        }

        [windowsTaskInstance: windowsTaskInstance]
    }

    def edit() {
        def windowsTaskInstance = WindowsTask.get(params.id)
        if (!windowsTaskInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'windowsTask.label', default: 'WindowsTask'), params.id])
            redirect(action: "list")
            return
        }

        [windowsTaskInstance: windowsTaskInstance]
    }

    def update() {
        def windowsTaskInstance = WindowsTask.get(params.id)
        if (!windowsTaskInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'windowsTask.label', default: 'WindowsTask'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (windowsTaskInstance.version > version) {
                windowsTaskInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'windowsTask.label', default: 'WindowsTask')] as Object[],
                          "Another user has updated this WindowsTask while you were editing")
                render(view: "edit", model: [windowsTaskInstance: windowsTaskInstance])
                return
            }
        }

        windowsTaskInstance.properties = params

        if (!windowsTaskInstance.save(flush: true)) {
            render(view: "edit", model: [windowsTaskInstance: windowsTaskInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'windowsTask.label', default: 'WindowsTask'), windowsTaskInstance.id])
        redirect(action: "show", id: windowsTaskInstance.id)
    }

    def delete() {
        def windowsTaskInstance = WindowsTask.get(params.id)
        if (!windowsTaskInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'windowsTask.label', default: 'WindowsTask'), params.id])
            redirect(action: "list")
            return
        }

        try {
            windowsTaskInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'windowsTask.label', default: 'WindowsTask'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'windowsTask.label', default: 'WindowsTask'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
